﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace GlobalGameJam
{
    class OptionsMenuScreen : AMenuScreen
    {
        MenuEntry soundMenuEntry;
        MenuEntry musicMenuEntry;

        static string[] sound = { "ON", "OFF" };
        public static int CurrentSound
        {
            get { return currentSound; }
        }
        static int currentSound = 0;
        static string[] music = { "ON", "OFF" };
        public static int CurrentMusic
        {
            get { return currentMusic; }
        }
        static int currentMusic = 0;

        ContentManager content;

        public OptionsMenuScreen()
            : base("Options")
        {
            soundMenuEntry = new MenuEntry(string.Empty);
            musicMenuEntry = new MenuEntry(string.Empty);

            SetMenuEntryText();

            MenuEntry back = new MenuEntry("Back");

            soundMenuEntry.Selected += SoundMenuEntrySelected;
            musicMenuEntry.Selected += MusicMenuEntrySelected;
            back.Selected += OnCancel;

            MenuEntries.Add(soundMenuEntry);
            MenuEntries.Add(musicMenuEntry);
            MenuEntries.Add(back);
            name = "OptionMenu";
        }



        void SetMenuEntryText()
        {
            soundMenuEntry.Text = "Sound: " + sound[currentSound];
            musicMenuEntry.Text = "Music: " + music[currentMusic];
        }

        void SoundMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentSound = (currentSound + 1) % sound.Length;
            SetMenuEntryText();
        }

        void MusicMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentMusic = (currentMusic + 1) % music.Length;
            SetMenuEntryText();
        }

        public override void HandleInput(InputState input)
        {
            if (input.IsMenuUp(ControllingPlayer))
            {
                selectedEntry--;

                if (selectedEntry < 0)
                    selectedEntry = menuEntries.Count - 1;
            }
            if (input.IsMenuDown(ControllingPlayer))
            {
                selectedEntry++;

                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0;
            }
            PlayerIndex playerIndex;
            if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
            {
                OnSelectEntry(selectedEntry, playerIndex);
            }
            else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
            {
                OnCancel(playerIndex);
            }
        }

        protected void OnCancel(object sender, PlayerIndexEventArgs e)
        {
            OnCancel(e.PlayerIndex);
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
        }
    }
}
